package com.woniuxy.community_life.controller;

import com.woniuxy.community_life.dto.CommentDto;
import com.woniuxy.community_life.dto.DynamicDto;
import com.woniuxy.community_life.pojo.*;
import com.woniuxy.community_life.service.DynamicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin
public class DynamicController {
    @Autowired
    private DynamicService dynamicService;

    @Value("${filepath}")
    private String filePath;

    /**
     * 社区汇发表动态
     * @return
     */
    @ResponseBody
    @RequestMapping("/activeDynamic")
    public Map addActiveDynamic(Dynamic dynamic, @RequestParam("upimage")MultipartFile[] imageFile) {

        Map result = new HashMap();
        try {
            dynamicService.addActiveDynamics(dynamic,filePath,imageFile);
            result.put("code", "200");
            result.put("msg", "发布成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code","500");
            result.put("msg","发布失败");
        }
        return result;
    }


    /**
     * 展示动态圈
     */
    @RequestMapping("/showDynamic")
    @ResponseBody
    public DynamicDto showDynamic(@RequestParam(value = "tRemitid",defaultValue ="1")Integer tRemitid){
        DynamicDto dynamicDto = new DynamicDto();
        dynamicDto=dynamicService.selectDynamic(tRemitid);
        return dynamicDto;
    }

    /*展示动态圈详情页*/
    @RequestMapping("/showDetailsDynamic")
    @ResponseBody
    public CommentDto showDetailsDynamic(Integer dId, Integer aId){
        CommentDto commentDto = new CommentDto();
        List<DynamicImage> imageList=dynamicService.selectImages(dId); //点赞人
        Account account=dynamicService.selectAccountNickName(aId);
        //查询回复、点赞量、评论人
        commentDto=dynamicService.selectAllMessage(dId);
        commentDto.setDynamicImages(imageList);
        commentDto.setAccount(account);
        return commentDto;
    }

    //回复评论功能
    @RequestMapping("/sendReplyComment")
    @ResponseBody
    public String sendReplyComment(Comment comment) {
        String result = null;
        Date date = null;
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = formatter.format(new Date());
            date = formatter.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        comment.setCommentTime(date);
        try {
            dynamicService.addReplyComment(comment);
            result = "success";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    //评论删除
    @RequestMapping("/deleteComment")
    @ResponseBody
    public String deleteComment(Integer comId,Integer aId){
        String result = null;
        try {
            dynamicService.removeComment(comId,aId);
            result = "success";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    //动态点赞
    @RequestMapping("/clickCount")
    @ResponseBody
    public Map clickCount(Integer dId,Integer aId) {
        Map map = new HashMap();
        try {
            dynamicService.addClickCount(dId,aId);
            map.put("code", "200");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", "500");
        }
        return map;
    }
}
